const cwd = process.cwd()
const path = require('path')
const webpack = require('webpack')
const HtmlPlugin = require('html-webpack-plugin')
const articlesData = require('../utils/handlerArticle')
const VueLoaderPlugin = require('vue-loader/lib/plugin')

const { THEME_PATH, DAIT_CONFIG } = global
const { site, configureWebpack } = DAIT_CONFIG

const _defineData = Object.assign({}, configureWebpack.defineData, {
  'ARTICLE_BASE': JSON.stringify(path.join(cwd, './articles')),
  'ARTIClES_DATA': JSON.stringify(articlesData),
  'SITE_CONFIG': JSON.stringify(site)
})

module.exports = {
  mode: 'none',
  entry: configureWebpack.entry,
  output: {
    filename: 'js/[name]-[hash:8].js',
    path: configureWebpack.output
  },
  resolve: {
    alias: configureWebpack.alias
  },
  module: {
    rules: [
      {
        test: /\.md$/,
        use: [
          {
            loader: 'html-loader',
            options: {
              attributes: {
                list: [
                  {
                    tag: 'img',
                    attribute: 'src',
                    type: 'src'
                  }
                ]
              }
            }
          },
          'marked-essay-loader'
        ]
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js$/,
        include: [
          THEME_PATH,
          path.resolve('../node_modules/element-ui/src'),
          path.resolve('../node_modules/element-ui/packages')
        ],
        use: [
          {
            loader: 'babel-loader'
          }
        ]
      },
      {
        test: /\.(jpg|jpeg|png)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 10 * 1024,
              name: 'assets/img/[name].[hash:8].[ext]'
            }
          }
        ]
      },
      {
        test: /\.(eot|woff|woff2|ttf)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'assets/fonts/[name].[ext]?[hash]'
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new webpack.DefinePlugin(_defineData),
    new HtmlPlugin({
      title: site.title,
      meta: {
        keywords: site.keywords || ''
      },
      template: path.join(cwd, './public/index.html')
    })
  ]
}
